package cn.uway.controller.demo;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.uway.cache.LocalCache;
import cn.uway.cache.RedisCache;
import cn.uway.pojo.demo.UserInfo;



@RestController
@RequestMapping("/testCache")
public class TestCacheController {

	@Autowired
	private LocalCache ehcache;
	
	@Autowired
	private RedisCache rediscache;
	
	@RequestMapping(value ="/redis/put" /*, method=RequestMethod.POST */)
    public String  putRedis(HttpServletRequest request, HttpServletResponse response, String userId) {
    	UserInfo user = new UserInfo();
        user.setUserName("张三");
        user.setLoginName("zhangsan");
        user.setPassword("BC69AE4A66EF117E38B5F047B8B2A3B9");
        user.setUserId(userId);;
    	rediscache.putObject("test.redis.user", user, 60); 	
    	
        return "ok";
    }
	
	@RequestMapping(value ="/redis/get" /*, method=RequestMethod.POST */)
    public Map<String,UserInfo>  getRedis(HttpServletRequest request, HttpServletResponse response, String userId) {
    	UserInfo user = (UserInfo)rediscache.getObject("test.redis.user"); 	
    	Map<String,UserInfo> map = new HashMap<String,UserInfo>();
    	map.put("userInfo", user);
        return map;
    }
	
	@RequestMapping(value ="/redis/info" )
    public Map<String,Object>  redisInfo(HttpServletRequest request, HttpServletResponse response, String userId) {
		long t = System.currentTimeMillis();
		Map<String,Object> map = new HashMap<String,Object>();
    	String info = rediscache.getRedisInfo(); 
    	if(info!=null && !"".equals(info)){
    		String[] array = info.split("\n");
    		for(String item : array){
    			if(!"".equals(item) && !item.startsWith("#") && item.contains(":")){
    				String[] tmp = item.split(":");
    				if(tmp.length==2){
    					map.put(tmp[0], tmp[1]);
    				}					
    			}
    		}
    	}
    	long t1 = System.currentTimeMillis();
    	System.out.println("================采集耗时："+(t1-t)+"ms");
    	return map;
    }
	
	@RequestMapping(value ="/ehcache/put" /*, method=RequestMethod.POST */)
    public String  putEhcache(HttpServletRequest request, HttpServletResponse response, String userId) {
    	UserInfo user = new UserInfo();
        user.setUserName("李四");
        user.setLoginName("lisi");
        user.setPassword("BC69AE4A66EF117E38B5F047B8B2B4C0");
        user.setUserId(userId);;
    	ehcache.putObjectWithExpireTime("test.ehcache.user", user, 60); 	
    	
        return "ok";
    }
	
	@RequestMapping(value ="/ehcache/get" /*, method=RequestMethod.POST */)
    public Map<String,UserInfo>  getEhcache(HttpServletRequest request, HttpServletResponse response, String userId) {
    	UserInfo user = (UserInfo)ehcache.getObject("test.ehcache.user"); 	
    	Map<String,UserInfo> map = new HashMap<String,UserInfo>();
    	map.put("userInfo", user);
        return map;
    }
    
	
}
